<?php
namespace app\index\controller;

use app\index\model\Apply;
use app\index\model\Vote;
use think\Cache;
use think\Config;
use think\Controller;
use think\Db;
use think\Env;
use think\File;
use think\Request;
use think\Session;
use wx\JsApiPay;
use wx\JSSDK;
use wx\WxPayJsApiPay;

class Index extends Controller
{
//    public function vote()
//    {
//
//    }

    public function getVote()
    {
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Max-Age:60');
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        header('Content-Type:application/json;charset=utf-8');
        $vote_count = Cache::get('vote_count');
        if (!$vote_count) {
            Cache::set('vote_count', 3);
        } else
            Cache::set('vote_count', $vote_count + 3);

        echo  Cache::get('vote_count')+ Cache::get('vote_add_count');

    }
    public function getMusicVote()
    {
        $music_vote_arr = Cache::get('music_vote_arr');
        if(count($music_vote_arr) <1 || !$music_vote_arr){
            $music_vote_arr = array(1=>0,2=>0,3=>0,4=>0,5=>0,6=>0);
            Cache::set('music_vote_arr', $music_vote_arr);
        }
        return $music_vote_arr;
    }
    public function setMusicVote($type,$ls_count=1)
    {
        $music_vote_arr = Cache::get('music_vote_arr');
        if(count($music_vote_arr) <1  || !$music_vote_arr)
            $music_vote_arr = array(1=>0,2=>0,3=>0,4=>0,5=>0,6=>0);
        $music_vote_arr[$type] = $music_vote_arr[$type]+$ls_count;
        Cache::set('music_vote_arr', $music_vote_arr);
    }

    public function addMusicVote($ls_count=10)
    {
        $music_vote_arr = Cache::get('music_vote_arr');
        if(!$music_vote_arr)
            return false;
        for($i=1; $i<=count($music_vote_arr) ; $i++)
        {
            $music_vote_arr[$i] =$music_vote_arr[$i]+$ls_count;
        }
        Cache::set('music_vote_arr', $music_vote_arr);
        return true;
    }

    public function getAreaVote()
    {
        $area_vote_arr = Cache::get('area_vote_arr');
        if(count($area_vote_arr) <1 || !$area_vote_arr){
            $area_vote_arr = array(1=>0,2=>0,3=>0,4=>0,5=>0,6=>0,7=>0,8=>0,9=>0,10=>0);
            Cache::set('area_vote_arr', $area_vote_arr);
        }
        return $area_vote_arr;
    }
    public function setAreaVote($type,$ls_count=1)
    {
        $area_vote_arr = Cache::get('area_vote_arr');
        if(count($area_vote_arr) <1 || !$area_vote_arr)
            $area_vote_arr = array(1=>0,2=>0,3=>0,4=>0,5=>0,6=>0,7=>0,8=>0,9=>0,10=>0);
        $area_vote_arr[$type] = $area_vote_arr[$type]+$ls_count;
        Cache::set('area_vote_arr', $area_vote_arr);
    }
    public function addAreaVote($ls_count=10)
    {
        $area_vote_arr = Cache::get('area_vote_arr');
        if(!$area_vote_arr)
            return false;
        for($i=1; $i<=count($area_vote_arr) ; $i++)
        {
            $area_vote_arr[$i] =$area_vote_arr[$i]+$ls_count;
        }
        Cache::set('area_vote_arr', $area_vote_arr);
        return true;
    }


//    public function setVote()
//    {
//
//        $shuzhi = $this->jisuan(5000,'2018-11-28 00:00:00','minute',3);
//        Cache::set('vote_add_count',$shuzhi);
//    }
//
//    private  function jisuan($total_people, $begin_time = '2018-11-28 00:00:00', $type = 'minute',$peishu=1)
//    {
//        if ($type = 'minute') {
//            $time_str = time() - strtotime($begin_time);
//            $minute_add_count = $total_people / 1440;
//            return  intval($time_str / 60 * $minute_add_count) * $peishu;
//        }
//        return 0;
//    }
}
